package com.rs.qm.modules.sys.controller;

import com.rs.qm.common.utils.R;
import com.rs.qm.modules.sys.entity.SysAreaEntity;
import com.rs.qm.modules.sys.service.SysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 行政区划
 *
 * @author GuoXunMin
 * @email v450625169@126.com
 * @date 2019-12-27 17:22:20
 */
@Configuration
@RestController
@RequestMapping("pc/sysarea")
public class SysAreaController {
    @Value("${static-data.hot-citys}")
    private String[] hotCtiys;

    @Autowired
    private SysAreaService sysAreaService;

    /**
     * 省信息
     */
    @GetMapping("/provinces")
    public R provinces(){
        List<SysAreaEntity> provinces = sysAreaService.queryProvinces();
        List<Map<String,String>> provinceMap = provinces.stream().map(sysAreaEntity -> {
            Map<String,String> map = new HashMap<>();
            map.put("areaCode",sysAreaEntity.getAreaCode());
            map.put("name",sysAreaEntity.getName());
            return map;
        }).collect(Collectors.toList());
        provinceMap = provinceMap.stream().filter(stringStringMap -> !stringStringMap.get("name").contains("市")).collect(Collectors.toList());
        return R.okData(provinceMap);
    }

    /**
     * 获取省的所有城市信息
     */
    @GetMapping("/{provincecode}/ctiys")
    public R province(@PathVariable(value = "provincecode") String provinceCode){
        List<SysAreaEntity> provinces = sysAreaService.queryCtiysByProvinceCode(provinceCode);
        Object[] ctiys = provinces.stream().map(sysAreaEntity -> sysAreaEntity.getName()).toArray();
        return R.okData(ctiys);
    }

    /**
     * 获取省的所有城市信息
     */

    //@CrossOrigin(origins = "*")
    @GetMapping("/hot/ctiys")
    public R hotCtiys(){
        return R.okData(hotCtiys);
    }

    /**
     * 列表
     *//*
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysAreaService.queryPage(params);

        return R.ok().put("page", page);
    }


    *//**
     * 信息
     *//*
    @RequestMapping("/info/{areaId}")
    public R info(@PathVariable("areaId") Long areaId){
		SysAreaEntity sysArea = sysAreaService.selectById(areaId);

        return R.ok().put("sysArea", sysArea);
    }

    *//**
     * 保存
     *//*
    @RequestMapping("/save")
    public R save(@RequestBody SysAreaEntity sysArea){
		sysAreaService.insert(sysArea);

        return R.ok();
    }

    *//**
     * 修改
     *//*
    @RequestMapping("/update")
    public R update(@RequestBody SysAreaEntity sysArea){
		sysAreaService.updateById(sysArea);

        return R.ok();
    }

    *//**
     * 删除
     *//*
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] areaIds){
		sysAreaService.deleteBatchIds(Arrays.asList(areaIds));

        return R.ok();
    }*/

}
